package com.license.logic.dao;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.license.common.util.Constants;
import com.license.logic.pojo.SmallEmployee;

public class EmployeeDAO {
	public List<SmallEmployee> getAllEmployeesNames(String directoryPath){
		List<SmallEmployee> smallEmployees = new ArrayList<SmallEmployee>();
		File[] files = new File(directoryPath).listFiles();
		int count = -1;
		for (File file : files) {
		    if (file.isFile()) {
		    	count ++;
		    	SmallEmployee employee = new SmallEmployee();
		    	employee.setUsername(file.getName().replace(".xml", ""));
		    	smallEmployees.add(employee);
		    }
		}
		
		return smallEmployees;
	}
	
	public List<SmallEmployee> getEmployeesNames(String directoryPath, int pageNumber){
		List<SmallEmployee> smallEmployees = new ArrayList<SmallEmployee>();
		File[] files = new File(directoryPath).listFiles();
		int minValue = pageNumber*Constants.EMPLOYEE_PAGINATION_NUMBER;
		int maxVlue = (pageNumber+1)*Constants.EMPLOYEE_PAGINATION_NUMBER < files.length ? (pageNumber+1)*Constants.EMPLOYEE_PAGINATION_NUMBER : files.length;
		int count = -1;
		for (int i = minValue ; i < maxVlue ; i++) {
		    if (files[i].isFile()) {
		    	count ++;
		    	SmallEmployee employee = new SmallEmployee();
		    	employee.setUsername(files[i].getName().replace(".xml", ""));
		    	smallEmployees.add(employee);
		    }
		}
		
		return smallEmployees;
	}
	
	public int countAllEmployee(String directoryPath){
		List<SmallEmployee> smallEmployees = new ArrayList<SmallEmployee>();
		File[] files = new File(directoryPath).listFiles();
		return files.length;
	}
}
